<script setup lang="ts">
  import { getarchiveList, deletearch, getCabinetList } from '@/api/archives';
  import {
    getarchiveCategory,
    getarchiveCategorysss,
    getArchiveStoreLife,
  } from '@/api/dataCenter';
  import useLoading from '@/hooks/useLoading';
  import { Message, Modal, TableInstance } from '@arco-design/web-vue';
  import { omit } from 'lodash';
  import { ref } from 'vue';
  import Edit from './edit/index.vue';
  import Add from './add/index.vue';
  import ImportExcel from './import/index.vue';
  import { useRoute } from 'vue-router';
  const proTableRef = ref<TableInstance | null>(null);
  const route = useRoute();
  const visible = ref(false);
  const visible1 = ref(false);
  const visible2 = ref(false);
  defineOptions({ name: 'danganzhonghe' });
  const { loading, setLoading } = useLoading(true);
  setLoading(true);
  const roomOptions = ref<any>(null);

  const fetchRoomData = async () => {
    const data = await getCabinetList(null);
    const filteredData = data.data.list.map((item: any) => ({
      label: item.cabinet_number,
      value: item.cabinet_id,
    }));
    roomOptions.value = filteredData;
  };
  const arc_type = ref();
  onMounted(() => {
    fetchRoomData();
    arc_type.value = route.meta.type;
  });
  const currentParams = ref({});
  async function request(params: any) {
    const res = await getarchiveList({
      ...params,
      arc_type: arc_type.value,
    });

    return {
      data: res.data,
    };
  }
  const SearchType = ref();
  const tableType = [
    {
      title: '类型',
      dataIndex: 'arc_type',
      slotName: 'tags',
      align: 'center',
      options: [
        {
          value: 1,
          label: '综合档案11111111',
          color: 'green',
        },
        {
          value: 2,
          label: '证照档案',
          color: 'magenta',
        },
        {
          value: 3,
          label: '人事档案',
          color: 'red',
        },
        {
          value: 4,
          label: '财务档案',
          color: 'gold',
        },
        {
          value: 5,
          label: '申报档案',
          color: 'cyan',
        },
        {
          value: 6,
          label: '技术档案',
          color: 'arcoblue',
        },
      ],
    },
    { dataIndex: 'arc_id', slotName: 'id' },
    {
      title: '档案编号',
      dataIndex: 'arc_code',
    },
    {
      dataIndex: 'arc_name',
      title: '档案名称',
    },
    {
      title: '目录号',
      dataIndex: 'arc_dir_code',
    },

    {
      title: '类别号',
      dataIndex: 'arc_cate_cod_name',
      align: 'center',
    },
    {
      title: '类别',
      dataIndex: 'arc_cate_name',
      align: 'center',
    },
    {
      title: '保管期限 ',
      dataIndex: 'arc_store_life_id',
      align: 'center',
    },
    {
      title: '单位名称',
      dataIndex: 'arc_company',
      align: 'center',
    },
    {
      title: '所属年份',
      dataIndex: 'arc_year',
      align: 'center',
    },
    {
      title: '所属月份',
      dataIndex: 'arc_month',
      align: 'center',
    },
    {
      title: '存放位置',
      children: [
        {
          title: '所属档案柜',
          dataIndex: 'arc_cabinet_name',
          align: 'center',
        },
        {
          title: '栏',
          dataIndex: 'arc_cabinet_row',
          align: 'center',
        },
        {
          title: '格',
          dataIndex: 'arc_cabinet_col',
          align: 'center',
        },
      ],
    },
    {
      title: '是否借出-',
      dataIndex: 'user_remarks',
      slotName: 'status',
      options: [
        {
          status: 'success',
          value: 1,
          label: '已借出',
        },
        {
          status: 'danger',
          value: 2,
          label: '未借出',
        },
      ],
    },
    {
      title: '操作',
      width: 130,
      fixed: 'right',
      slotName: 'operations',
      menus: [
        {
          name: '借出',
          onClick: (record: any) => {
            deletearch({
              arc_ids: [record.arc_id],
            }).then((res: any) => {
              proTableRef.value?.setRefresh();
              Message.success('删除成功！');
            });
          },
        },
        {
          name: '编辑',
          onClick: (record: any) => {
            visible1.value = true;
            currentParams.value = {
              id: record.arc_id,
            };
          },
        },
        {
          name: '删除',
          onClick: (record: any) => {
            deletearch({
              arc_ids: [record.arc_id],
            }).then((res: any) => {
              proTableRef.value?.setRefresh();
              Message.success('删除成功！');
            });
          },
        },
      ],
    },
  ];
  const sonFormType = ref<any>();

  getarchiveCategory().then((res) => {
    getarchiveCategorysss().then((resa) => {
      getArchiveStoreLife().then((resaa) => {
        sonFormType.value = [
          {
            name: 'arc_code',
            label: '档案编号',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
          },
          {
            name: 'arc_name',
            label: '档案名称',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
          },
          // {
          //   name: 'arc_type',
          //   label: '档案类型',
          //   type: 'a-select',
          //   fieldProps: {
          //     span: 24,
          //     allowSearch: true,
          //     fieldNames: { value: 'cvalue', label: 'clabel' },
          //   },
          //   selectOptions: [
          //     {
          //       clabel: '综合档案',
          //       cvalue: 1,
          //     },
          //     {
          //       clabel: '证照档案',
          //       cvalue: 2,
          //     },
          //     {
          //       clabel: '人事档案',
          //       cvalue: 3,
          //     },
          //     {
          //       clabel: '财务档案',
          //       cvalue: 4,
          //     },
          //     {
          //       clabel: '申报档案',
          //       cvalue: 5,
          //     },
          //     {
          //       clabel: '技术档案',
          //       cvalue: 6,
          //     },
          //   ],
          // },
          {
            name: 'arc_dir_code',
            label: '档案目录号',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
          },
          {
            name: 'arc_cate_id',
            label: '档案类别',
            type: 'a-select',
            fieldProps: {
              span: 24,
              allowSearch: true,
              fieldNames: { value: 'cate_id', label: 'cate_name' },
            },
            selectOptions: res.data.list,
          },
          {
            name: 'arc_cate_code_id',
            label: '档案类别号',
            type: 'a-select',
            fieldProps: {
              span: 24,
              allowSearch: true,
              fieldNames: { value: 'code_id', label: 'code_name' },
            },
            selectOptions: resa.data.list,
          },
          {
            name: 'arc_store_life_id',
            label: '档案保管年限',
            type: 'a-select',
            fieldProps: {
              span: 24,
              allowSearch: true,
              fieldNames: { value: 'store_id', label: 'store_name' },
            },
            selectOptions: resaa.data.list,
          },
          {
            name: 'arc_company',
            label: '单位名称',
            type: 'a-input',
            fieldProps: {
              span: 24,
            },
          },
          {
            name: 'arc_year',
            label: '所属年月份',
            type: 'a-month-picker',
            fieldProps: {
              span: 24,
            },
          },
          {
            name: 'arc_cabinet_id',
            label: '所属档案柜',
            type: 'a-select',
            fieldProps: {
              span: 24,
              placeholder: '请选择所属档案柜',
            },
            selectOptions: roomOptions,
          },
          {
            name: 'arc_cabinet_row',
            label: '柜号',
            type: 'a-input',
            fieldProps: {
              span: 12,
            },
          },
          {
            name: 'arc_cabinet_col',
            label: '格号',
            type: 'a-input',
            fieldProps: {
              span: 12,
            },
          },
          {
            name: 'arc_sort',
            label: '排序',
            type: 'a-input-number',
            fieldProps: {
              span: 24,
            },
          },
        ];
        SearchType.value = [
          {
            name: 'arc_code',
            label: '档案编号',
            type: 'a-input',
            fieldProps: {
              span: 8,
            },
          },
          {
            name: 'arc_name',
            label: '档案名称',
            type: 'a-input',
            fieldProps: {
              span: 8,
            },
          },
          {
            name: 'arc_dir_code',
            label: '目录号',
            type: 'a-input',
            fieldProps: {
              span: 8,
            },
          },
          {
            name: 'arc_cate_id',
            label: '档案类别',
            type: 'a-select',
            fieldProps: {
              span: 8,
              allowSearch: true,
              fieldNames: { value: 'cate_id', label: 'cate_name' },
            },
            selectOptions: res.data.list,
          },
          {
            name: 'arc_cate_code_id',
            label: '档案类别号',
            type: 'a-select',
            fieldProps: {
              span: 8,
              allowSearch: true,
              fieldNames: { value: 'code_id', label: 'code_name' },
            },
            selectOptions: resa.data.list,
          },
          {
            name: 'arc_store_life_id',
            label: '档案保管年限',
            type: 'a-select',
            fieldProps: {
              span: 8,
              allowSearch: true,
              fieldNames: { value: 'store_id', label: 'store_name' },
            },
            selectOptions: resaa.data.list,
          },
          {
            name: 'arc_cabinet_id',
            label: '所属档案柜',
            type: 'a-select',
            fieldProps: {
              span: 8,
            },
            selectOptions: roomOptions,
          },
        ];
        setLoading(false);
      });
    });
  });

  function handleRefresh() {
    loading.value = true;
    proTableRef.value?.setRefresh();
    loading.value = false;
  }
</script>

<template>
  <div class="container">
    <pro-table
      v-if="!loading"
      ref="proTableRef"
      :formType="SearchType"
      title="综合档案"
      :request="request"
      :tableType="tableType"
    >
      <template #btns>
        <a-space>
          <a-button
            type="primary"
            @click="
              () => {
                visible = true;
              }
            "
          >
            <template #icon>
              <icon-plus-circle-fill />
            </template>
            添加档案
          </a-button>
          <a-button
            type="outline"
            @click="
              () => {
                visible2 = true;
              }
            "
          >
            <template #icon>
              <icon-import />
            </template>
            批量导入
          </a-button>
        </a-space>
      </template>
      <template #options>
        <Add
          v-if="visible"
          v-model:visible="visible"
          :form-type="sonFormType"
          :type="arc_type"
          @refresh="handleRefresh"
        />
        <Edit
          v-if="visible1"
          v-model:visible="visible1"
          :params="currentParams"
          :form-type="sonFormType"
          :type="arc_type"
          @refresh="handleRefresh"
        />
        <ImportExcel v-if="visible2" v-model:visible="visible2" />
      </template>
    </pro-table>
    <div
      v-else-if="loading"
      style="
        width: 100%;
        height: 700px;
        display: flex;
        justify-content: center;
        align-items: center;
      "
    >
      <a-spin :size="10" dot />
    </div>
  </div>
</template>

<style scoped lang="less">
  .formContent {
    width: 100%;
  }
  // tree的css样式
  :deep(.icon) {
    width: 1.2em !important;
    height: 1.2em !important;
  }

  :deep(.arco-tree-node-title-text) {
    font-size: 12px;
    width: 140px;
    overflow: hidden; /* 隐藏溢出内容 */
    white-space: nowrap; /* 禁止换行 */
    text-overflow: ellipsis; /* 显示省略号 */
  }

  :deep(.arco-tree-node-indent-block) {
    width: 0px !important;
  }
  .item {
    display: flex;
    flex-direction: row;
    align-items: center;
  }
</style>
